class BuildingSelectList extends React.Component {
    constructor(props) {
        super(props);
        this.commList = [];
        this.selectId = Math.random().toString(36).substr(3);
    }

    get value() {
        return this.getDefCommCode();
    }

    get commName() {
        let selectedIndex = this.refs[this.selectId].selectedIndex;
        return this.refs[this.selectId][selectedIndex].dataset.commname;
    }

    layuiInitComplete(queryComm) {
        this.form = layui.form();
        this.form.on('select(' + this.selectId + ')', function (data) {
            this.selectCallback(data.value);
        }.bind(this));

        if (queryComm) {
            this.queryList();
        }
    }

    componentDidUpdate() {
        this.form = layui.form();
        if (this.props.defCommCode) {
            // noinspection JSPrimitiveTypeWrapperUsage
            this.refs[this.selectId].value = this.props.defCommCode;
        }
        this.form.render('select');
    }

    queryList() {
        postJson("/comm/findCommList.x", null, function (data) {
            this.commList = data.data;
            this.setState({}, () => {
                this.selectCallback(this.refs[this.selectId].value);
            });
        }.bind(this));
    }

    selectCallback(commCode) {
        if (this.props.selectCallback)
            this.props.selectCallback(commCode);
    }

    getDefCommCode() {
        return this.refs[this.selectId].value;
    }

    commOption() {
        let option = [];
        const array = this.commList;
        const length = this.commList.length;
        if (length > 0) {
            if (length !== 1 && this.props.isDef) {
                option.push(<option value="-1">请选择小区</option>)
            }
            for (let k in array) {
                option.push(<option
                    value={array[k].commCode}
                    data-commname={array[k].commName}>{length !== 1 ? '【' + array[k].commCode + '】' + array[k].commName : array[k].commName
                }</option>)
            }
        } else {
            option.push(<option value="-1">请选择小区</option>)
        }

        return option;
    }

    render() {
        return !this.props.isNotItem ? <div className="layui-form-item">
                <label className="layui-form-label" style={this.props.labelStyle ? this.props.labelStyle : {}}>选择小区</label>
                <div className="layui-input-inline" style={this.props.inputStyle ? this.props.inputStyle : {}}>
                    <select name={this.selectId} ref={this.selectId} data-filter={this.selectId}>
                        {this.commOption()}
                    </select>
                </div>
            </div> :
            <div>
                <label className="layui-form-label"
                       style={this.props.labelStyle ? this.props.labelStyle : {}}>选择小区</label>
                <div className="layui-input-inline" style={this.props.inputStyle ? this.props.inputStyle : {}}>
                    <select name={this.selectId} ref={this.selectId} data-filter={this.selectId}>
                        {this.commOption()}
                    </select>
                </div>
            </div>
    }
}